package com.baizhi.service.impl;

import com.baizhi.dao.CmfzAlbumDao;
import com.baizhi.entity.CmfzAlbum;
import com.baizhi.service.CmfzAlbumService;
import com.baizhi.util.FileUpload;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/4/9 0009.
 */
@Service
public class CmfzAlbumServiceImpl implements CmfzAlbumService {
    @Autowired
    private CmfzAlbumDao cmfzAlbumDao;

    @Override
    public Map showAll(Integer page, Integer rows) {
        Map map=new HashMap();

        IPage<CmfzAlbum> iPage=new Page<>(page,rows);
        IPage<CmfzAlbum> cmfzAlbumIPage = cmfzAlbumDao.selectPage(iPage, null);
        List<CmfzAlbum> records = cmfzAlbumIPage.getRecords();
        long total = cmfzAlbumIPage.getTotal();

        map.put("total", total);
        map.put("rows", records);

        return map;
    }

    @Override
    public Map add(MultipartFile picture, CmfzAlbum cmfzAlbum, HttpServletRequest request) {
        Map map = new HashMap();
        try {
            FileUpload.uploadAlbumPicture(picture,cmfzAlbum,request);
            cmfzAlbumDao.add(cmfzAlbum);
            map.put("flag", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag", false);
        }
        return map;
    }

    @Override
    public Map update(MultipartFile picture, CmfzAlbum cmfzAlbum, HttpServletRequest request) {
        Map map = new HashMap();
        try {
            FileUpload.updateAlbumPicture(picture,cmfzAlbum,request);
            cmfzAlbumDao.updateById(cmfzAlbum);
            map.put("flag", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag", false);
        }
        return map;
    }
}
